package com.zzk.lanouhn.myapplication1.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.modles.ShowKindsDetails;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;

/**
 * Message页面->晒单->分类下各个item的详情，晒单详情
 * Created by lanouhn on 16/9/7.
 */
public class ShowKindsDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_back, iv_icon_author, iv_goodsPic, iv_comment, iv_like, iv_share, iv_more;
    private ImageView iv_share_pic;
    private TextView tv_name_author, tv_contents, tv_title_goods, tv_price_goods, tv_likes, tv_count;
    private RelativeLayout rl;
    private String id;
    private ShowKindsDetails kindsDetails;
    private LinearLayout ll_show, ll_comments;
    private SharedPreferences share;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showkindsdetails);
        initView();
        initData();
        initEvent();
        getData();


    }


    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back_show_kinds);
        iv_icon_author = (ImageView) findViewById(R.id.iv_icon_show_kinds_details);
        iv_goodsPic = (ImageView) findViewById(R.id.iv_goodPic_show_kind_details);
        iv_comment = (ImageView) findViewById(R.id.iv_details_comments_show);
        iv_like = (ImageView) findViewById(R.id.iv_details_like_show);
        iv_share = (ImageView) findViewById(R.id.iv_details_share_show);
        iv_more = (ImageView) findViewById(R.id.iv_details_more_show);
//        iv_share_pic= (ImageView) findViewById(R.id.iv_pic_show_kinds_details);

        tv_name_author = (TextView) findViewById(R.id.tv_name_show_kinds_details);
        tv_contents = (TextView) findViewById(R.id.tv_contents_show_kinds_details);
//        tv_title_goods= (TextView) findViewById(R.id.tv_title_show_kinds_details);
//        tv_price_goods= (TextView) findViewById(R.id.tv_price_show_kinds_details);
        tv_likes = (TextView) findViewById(R.id.tv_likes_show_kinds_details);
        tv_count = (TextView) findViewById(R.id.tv_count_comments);
        rl = (RelativeLayout) findViewById(R.id.rl_more);
        ll_show = (LinearLayout) findViewById(R.id.ll_show);
        ll_comments = (LinearLayout) findViewById(R.id.ll_comments_details_show);
    }

    private void initData() {
        Bundle bundle = this.getIntent().getExtras();
        id = bundle.getString("goodsid");


    }

    private void initEvent() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });
        iv_icon_author.setOnClickListener(this);
        tv_name_author.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_icon_show_kinds_details:
            case R.id.tv_name_show_kinds_details:
                Intent intent = new Intent(ShowKindsDetailsActivity.this, AuthorsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("authorId", kindsDetails.getData().getPost().getAuthor_id());
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            default:

                break;
        }
    }

    private void getData() {
        String url = "http://open4.bantangapp.com/community/post/info?";
        String dataUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2" +
                "&screensize=720&v=19&id=" + id + "&trace_id=";
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    kindsDetails = gson.fromJson(result, ShowKindsDetails.class);
                    share = getSharedPreferences("user", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = share.edit();
                    String authorId = kindsDetails.getData().getPost().getAuthor_id();
                    editor.putString("AuthorId", authorId);
                    editor.commit();
                    if (kindsDetails.getData().getPost() != null) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    ShowKindsDetails.DataBean.PostBean bean = kindsDetails.getData().getPost();
//                    if (bean.getTags().size() <= 0) {
//                        rl.setVisibility(View.GONE);
//                    } else {
//                        rl.setVisibility(View.VISIBLE);
//                    }
                    Picasso.with(ShowKindsDetailsActivity.this).load(bean.getAuthor().getAvatar()).into(iv_icon_author);
                    Picasso.with(ShowKindsDetailsActivity.this).load(bean.getMiddle_pic_url()).into(iv_goodsPic);
                    tv_name_author.setText(bean.getAuthor().getNickname());
                    tv_contents.setText(bean.getContent());
                    tv_contents.setLineSpacing(1, 1.5f);
                    if (bean.getType_id().equals("1")) {
                        View view1 = LayoutInflater.from(ShowKindsDetailsActivity.this).inflate(R.layout.item_list_details, null);
                        iv_share_pic = (ImageView) view1.findViewById(R.id.iv_item_pic_details);
                        tv_title_goods = (TextView) view1.findViewById(R.id.tv_item_name_details);
                        tv_price_goods = (TextView) view1.findViewById(R.id.tv_item_price_details);
                        RelativeLayout rel= (RelativeLayout) view1.findViewById(R.id.rl_to_single_shop);
                        RelativeLayout rel_buy= (RelativeLayout) view1.findViewById(R.id.rel_buy);
                        Picasso.with(ShowKindsDetailsActivity.this).load(bean.getBrand_product().get(0).getPic()).into(iv_share_pic);
                        tv_title_goods.setText(bean.getBrand_product().get(0).getTitle());
                        tv_price_goods.setText(bean.getBrand_product().get(0).getPrice());
                        tv_likes.setText(bean.getBrand_product().get(0).getLikes());
                        rel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent=new Intent(ShowKindsDetailsActivity.this,SingleShopDetailsActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putString("singleId",kindsDetails.getData().getPost().getBrand_product().get(0).getId());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                        rel_buy.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent=new Intent(ShowKindsDetailsActivity.this,MessageArticleGuide.class);
                                intent.putExtra("guideUrl",kindsDetails.getData().getPost().getBrand_product().get(0).getUrl());
                                startActivity(intent);
                            }
                        });
                        ll_show.addView(view1);

                    }
                    if (bean.getType_id().equals("0") && bean.getProduct().size() > 0) {
                        View view2 = LayoutInflater.from(ShowKindsDetailsActivity.this).inflate(R.layout.item_connect_kindsdetails, null);
                        RelativeLayout rel_link= (RelativeLayout) view2.findViewById(R.id.rel_link);
                        iv_share_pic = (ImageView) view2.findViewById(R.id.iv_pic_taobao_show);
                        tv_title_goods = (TextView) view2.findViewById(R.id.tv_title_taobao_show);
                        tv_price_goods = (TextView) view2.findViewById(R.id.tv_price_taobao_show);
                        Picasso.with(ShowKindsDetailsActivity.this).load(bean.getProduct().get(0).getPic()).into(iv_share_pic);
                        tv_title_goods.setText(bean.getProduct().get(0).getTitle());
                        tv_price_goods.setText(bean.getProduct().get(0).getPrice());
                        tv_likes.setText(bean.getDynamic().getLikes());
                        ll_show.addView(view2);
                        rel_link.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent=new Intent(ShowKindsDetailsActivity.this,MessageArticleGuide.class);
                                intent.putExtra("guideUrl",kindsDetails.getData().getPost().getProduct().get(0).getUrl());
                                startActivity(intent);
                            }
                        });
                    }
                    if (bean.getComments() != null) {
                        tv_count.setText("查看 共有" + bean.getComments().size() + "条评论");
                        int number = bean.getComments().size();
                        int size = 0;
                        if (number >= 3) {
                            size = 3;
                        } else if (number < 3) {
                            size = number;
                        }
                        for (int i = 0; i < size; i++) {
                            View view = LayoutInflater.from(ShowKindsDetailsActivity.this).inflate(R.layout.item_comments_show, null);
                            TextView tv_userName = (TextView) view.findViewById(R.id.tv_name_comments_item);
                            TextView tv_comments_content = (TextView) view.findViewById(R.id.tv_comments_content_show);

                            String authorname = bean.getComments().get(i).getUsername();
                            String username = bean.getComments().get(i).getAt_user().getNickname();
                            tv_userName.setText(authorname + ":");
                            if (username != null) {
                                String content = "回复 @" + username + "" + bean.getComments().get(i).getConent();
                                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);
                                spannableStringBuilder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.mycolor)), 2, username.length() + 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                tv_comments_content.setText(spannableStringBuilder);
                            } else {
                                tv_comments_content.setText(bean.getComments().get(i).getConent());
                            }
                            ll_comments.addView(view);

                        }
                    }


                    break;
                default:

                    break;
            }
        }
    };


}
